<?php
class Lwn_gradeAction extends IntesysAction {
	
	public function index(){
		
		$total = $this->grade();
		$this->assign('totalgrade',$total['positive']);
		
		$this->assign('allid',$total['minus']);
		
		$this->assign('depid',$total['count']);
		$this->display('Analysis:total');
		}
		
	public function grade(){
		
		
	   //time 传 y m w  
		$total['memberinfo']=session('memberInfo');
		if($this->_param('time')!=null){
		   $date=$this->_param('time');	
			}
			
		$show='position';	
		if($this->_param('show')!=null){
		   $show=$this->_param('show');	
		}
		
		if($this->_param('target')!=null){
			$target=$this->_param('target');
		}	
				
         $time=$this->l_time($date);
		 $start_time=$time['start_time'];
		 $out_time=$time['out_time'];
		if($show=='site'){
			$count_where='site_id = '.__SITEID__.' and dateline > '.$start_time.' and dateline < '.$out_time;
			$count_field='count(*)';
			$minus_field='sum(num)';
			$minus_where='site_id = '.__SITEID__.'  and num < 0  and dateline > '.$start_time.' and dateline < '.$out_time;
			$positive_where='site_id = '.__SITEID__.' and num > 0 and dateline > '.$start_time.' and dateline < '.$out_time;
			$positive_field='sum(num)';		
		}else if($show=='position'){
			if($target==null){
			   if($total['memberInfo']['memberpositionid']!=''){
				    $target=$total['memberInfo']['memberpositionid'];
			   }else{
				    $mp=M('Member')->where('site_id = '.__SITEID__)->Field('memberpositionid')->Find();
				    $target=$mp['memberpositionid'];
			   }
			}
			$this->assign('lwn_template',2);
		
			$count_where='site_id = '.__SITEID__.' and memberpositionid like "%'.$target.'%" and dateline > '.$start_time.' and dateline < '.$out_time;
			$count_field='count(*)';
			$minus_field='sum(num)';
			$minus_where='site_id = '.__SITEID__.' and memberpositionid like "%'.$target.'%" and num < 0 and dateline > '.$start_time.' and dateline < '.$out_time;
			$positive_where='site_id = '.__SITEID__.' and memberpositionid like "%'.$target.'%" and num > 0 and dateline > '.$start_time.' and dateline < '.$out_time;
			$positive_field='sum(num)';
		}
		
		
		
		
		//$member=M('Member')->where('site_id = '.__SITEID__.' and memberpositionid like "%'.$memberInfo['memberpositionid'].'%"')->select();
	
		
		$total['count']=M('integralranking')->where($count_where)->getField($count_field);
        $total['minus']=M('integralranking')->where($minus_where)->getField($minus_field);
		$total['positive']=M('integralranking')->where($positive_where)->getField($positive_field);
		//dump($total);
		if($total['count']==null){
			$total['count']='--';
			}
		if($total['minus']==null){
			$total['minus']='--';
			}
		if($total['positive']==null){
			$total['positive']='--';
			}
	
		
	
		
		
		
        return $total;
		
		
			
			}

	public function l_time($time){
		
		
		if($time=='y'){
			$year=date("Y",time());
            $first=$year."-01-01";
            $end=$year."-12-31";
			$start_time=strtotime($first);
			$out_time=strtotime($end);
		}else if($time=='m'){
			$start_time=date('Y-m-01', strtotime(date("Y-m-d")));
			$out_time=date('Y-m-d', strtotime("$start_time +1 month -1 day"));
			$start_time=strtotime($start_time);
			$out_time=strtotime($out_time);
		}else if($time=='w'){
          $date=new DateTime();
          $date->modify('this week');
          $start_time=$date->format('Y-m-d');
          $date->modify('this week +6 days');
          $out_time=$date->format('Y-m-d');
	      $start_time=strtotime($start_time);
		  $out_time=strtotime($out_time);
		}else{
			$start_time = strtotime(date('Ymd'));	
			$out_time = strtotime(date('Ymd')) + 86400;
         }
		
		return array('start_time'=>$start_time,'out_time'=>$out_time);
		
		}
	
		
	
}

